package com.yuwei.user

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Parcel
import android.os.Parcelable
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.PopupWindow
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.yuwei.base.RoutePath
import com.yuwei.common.mvi.BaseViewActivity
import com.yuwei.user.databinding.ActivityPhoneLoginBinding
import com.yuwei.user.entity.GetPermissions

@Route(path = RoutePath.MODULE_PHONE_LOGIN_ACTIVITY)
class PhoneLoginActivity: BaseViewActivity<ActivityPhoneLoginBinding>(){


    override fun initView() {
        if (GetPermissions.regexPhoneNumber(binding.btnGetPhoneCode.text.toString())){
            binding.btnGetPhoneCode.setBackgroundColor(R.drawable.shape_phone_login_code1)
        }
        binding.ivBack.setOnClickListener {
            finish()
        }
        binding.tvHelp.setOnClickListener {
            ARouter.getInstance().build(RoutePath.MODULE_LOGIN_HELP_ACTIVITY).navigation()
        }
        val spannable = SpannableStringBuilder(binding.tvProtocol.text.toString())
        val spannable1 = SpannableStringBuilder(binding.tvPrivacy.text.toString())
        spannable.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0, 4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable1.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable.setSpan(object : ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(RoutePath.MODULE_LOGIN_WEB_ACTIVITY)
                    .withString("url","https://terms.alicdn.com/legal-agreement/terms/suit_bu1_unification/suit_bu1_unification202005142208_14749.html")
                    .navigation()
            }

        },0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvProtocol.setText(spannable)
        binding.tvProtocol.setMovementMethod(LinkMovementMethod.getInstance())
        spannable1.setSpan(object : ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(RoutePath.MODULE_LOGIN_WEB_ACTIVITY)
                    .withString("url","https://www.douyin.com/draft/douyin_agreement/douyin_agreement_privacy.html?id=6773901168964798477")
                    .navigation()
            }

        },0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvPrivacy.text = spannable1
        binding.tvPrivacy.movementMethod = LinkMovementMethod.getInstance()
//        val popupView = LayoutInflater.from(this).inflate(R.layout.popwindow_login, null)
//        val popupWindow = PopupWindow(
//            popupView,
//            ViewGroup.LayoutParams.MATCH_PARENT,
//            ViewGroup.LayoutParams.WRAP_CONTENT,
//            true
//        ).apply {
//            setBackgroundDrawable(ColorDrawable(Color.parseColor("#666666")))
//            popupView.findViewById<TextView>(R.id.cancle).setOnClickListener {
//                dismiss()
//            }
//        }
        binding.ivMore.setOnClickListener {
            XPopup.Builder(this)
                .enableDrag(true)
                .asCustom(object : BottomPopupView(this){
                    override fun getImplLayoutId(): Int = R.layout.popwindow_login
                    override fun onCreate() {
                        super.onCreate()
                        val cancel = findViewById<TextView>(R.id.cancel)
                        cancel.setOnClickListener {
                            dismiss()
                        }

                    }
                })
                .show()
        }
        binding.btnGetPhoneCode.setOnClickListener {
            if (GetPermissions.regexPhoneNumber(binding.etPhoneLogin.text.toString())){
                ARouter.getInstance().build(RoutePath.MODULE_CODE_LOGIN_ACTIVITY).navigation()
            }else{
                ToastUtils.showLong("手机号格式错误,请重新输入")
            }
        }
        binding.llPhoneCodeEt.setOnClickListener {
            ARouter.getInstance().build(RoutePath.MODULE_USER_LOGIN_ACTIVITY).navigation()
        }
    }

    override fun initData() {
        binding.etPhoneLogin.setOnClickListener {
            binding.etPhoneLogin.requestFocus()
            showKeyBoard(binding.etPhoneLogin)
        }
    }

    private fun showKeyBoard(view:View) {
        val imm = getSystemService (Context.INPUT_METHOD_SERVICE) as InputMethodManager
         imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }
}